#include "stdio.h"
#include "TsMux.h"
#include <string.h>
#include "h264_one_frame_get.h"
#include "aac_one_frame_get.h"

#define INV_FILE    "es.h264"
#define INA_FILE    "es.aac"
#define OUT_FILE    "test.ts"

#define READV_SIZE   (30*128)
#define READA_SIZE	 (   350)
#define CACHE_SIZE      100     //   size = ((CACHE_SIZE + 500) % 1024) * 1024     more than max frame size

int main()
{
    //parameter initialization
    printf("file:%s line:%d\n",__FILE__,__LINE__);
    int ret = 0;
    int i = 0;
    long long pts = 0;
    long long apts = 0;
    int buffer_video_l = 0;
    int buffer_audio_l = 0;
    int frame_num = 0;
    int read_long = READV_SIZE;
    char buffer_video[READV_SIZE] = {""};
    char buffer_audio[READA_SIZE] = {""};
    H264FileSource file_s;
    aac_get_info s_aac_info;
    char * dst_video_buffer_video = NULL;
	dst_video_buffer_video = (char *)malloc(10000000);
    
    //program initialization
    printf("file:%s line:%d\n",__FILE__,__LINE__);
    mpegts_write_header();
    h264_one_frame_get_init(&file_s, 1000);	
    aac_one_frame_get_init(&s_aac_info);
    
    //open es_file read
    printf("file:%s line:%d\n",__FILE__,__LINE__);
    FILE * fp_inv = fopen(INV_FILE,"rb");
    FILE * fp_ina = fopen(INA_FILE,"rb");
    FILE * fp_out = fopen(OUT_FILE,"wb");
    
    while(1)
    {
        memset(buffer_video,0,READV_SIZE);
        ret = fread(buffer_video,READV_SIZE,1,fp_inv);
        if(ret != 1)
        {
            printf("v ret = %d\n",ret);
            break;
        }
        memset(buffer_audio,0,READA_SIZE);
        ret = fread(buffer_audio,READA_SIZE,1,fp_ina);
        if(ret != 1)
        {
            printf("a ret = %d\n",ret);
            break;
        }
        
        buffer_video_l = 0;
        read_long = READV_SIZE;
        
		while(h264_one_frame_get_process(&file_s, (char *)buffer_video, read_long,dst_video_buffer_video, &buffer_video_l)!= 2)
		{
			if (buffer_video_l > 0)
			{
				//printf("xubinbin file:%s line:%d\n",__FILE__,__LINE__);
				//printf("frame_num = *%d*       buffer_video_l = *%d*\n",frame_num++,buffer_video_l);
                pts += 33*90;
                write_pes_packet(dst_video_buffer_video,buffer_video_l, pts, pts,AVMEDIA_TYPE_VIDEO);
                //printf("tsmuxmem.len = %d\n",tsmuxmem.len);
                fwrite(&tsmuxmem.buffer[0],tsmuxmem.len,1,fp_out);
                tsmuxmem.len = 0;
			}
			buffer_video_l = 0;
            read_long = 0;
		}
        
        buffer_audio_l = 0;
        read_long = READA_SIZE;
		while(aac_one_frame_get_process(&s_aac_info, (char *)buffer_audio, read_long,dst_video_buffer_video, &buffer_audio_l)!= 2)
		{
			if (buffer_audio_l > 0)
			{
				//printf("xubinbin file:%s line:%d\n",__FILE__,__LINE__);
				//printf("frame_num = *%d*       buffer_audio_l = *%d*\n",frame_num++,buffer_audio_l);
                apts += 23*90;
                write_pes_packet(dst_video_buffer_video,buffer_audio_l, apts, apts,AVMEDIA_TYPE_AUDIO);
                //printf("tsmuxmem.len = %d\n",tsmuxmem.len);
                fwrite(&tsmuxmem.buffer[0],tsmuxmem.len,1,fp_out);
                tsmuxmem.len = 0;
			}
			buffer_audio_l = 0;
            read_long = 0;
		}
    }

    fclose(fp_inv);
    fclose(fp_out);

    printf("Done !\n");
    return 0;
}
